package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.Group;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.repositories.GroupRepository;
import cn.thoughtworks.school.programCenter.services.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/programs")
public class UserGroupController {

    @Autowired
    private UserGroupService userGroupService;
    @Autowired
    private GroupRepository groupRepository;

    @Transactional
    @PutMapping(value = "{programId}/students/{studentId}/groups")
    public ResponseEntity editStudentGroups(@PathVariable Long programId,
                                          @PathVariable Long studentId,
                                          @Auth User current,
                                          @RequestBody Map data) {
        List<String> groups = (List<String>) data.get("groups");
        userGroupService.editTags(programId, studentId, current.getId(), groups);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @GetMapping("groups")
    public ResponseEntity getGroups() {
        List<Group> groups = groupRepository.findAll();

        return new ResponseEntity(groups, HttpStatus.OK);
    }
}
